import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ProfileServiceProxy, ProcessingDeliveryViewDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData } from '../../shared/organization-unit-tree.component';
import * as _ from 'lodash';

@Component({
  selector: 'ProcessingDeliverySubCreate',
  templateUrl: './ProcessingDeliverySubCreate.component.html',
  styleUrls: ['./ProcessingDeliverySubCreate.component.css']
})
export class ProcessingDeliverySubCreateComponent extends AppComponentBase {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  requestdate;
  active = false;
  saving = false;
  newdto: ProcessingDeliveryViewDto;
  _processingDeliveryViewDto: ProcessingDeliveryViewDto = new ProcessingDeliveryViewDto();
  constructor(injector: Injector) {
    super(injector);
  }

  show(dto: ProcessingDeliveryViewDto): void {
    console.log(dto);
    this.active = true;
    this._processingDeliveryViewDto = dto;
    this.requestdate = dto.progressRequest.format('YYYY-MM-DD');
    console.log(this._processingDeliveryViewDto);
    this.modal.show();
  }

  onShown(): void {
    //document.getElementById('Code').focus();
  }

  save(): void {
    console.log(this._processingDeliveryViewDto)
    this.modal.hide();
  }

  close(): void {
    this.active = false;
    this._processingDeliveryViewDto.deliveryAmount = 0;
    this._processingDeliveryViewDto.deliveryDate = null;
    this._processingDeliveryViewDto.passedAmount = 0;
    this._processingDeliveryViewDto.quantityAmount = 0;
    this._processingDeliveryViewDto.noPassedAmount = 0;
    this._processingDeliveryViewDto.qualityControlCardNo = '';
    this._processingDeliveryViewDto.isOverdue = null;
    this._processingDeliveryViewDto.overdueDay = 0;
    this._processingDeliveryViewDto.remarks = '';
    this.modal.hide();
  }

  dateDifference(): void {
    // Calculate time between two dates:
    const date1: any = this._processingDeliveryViewDto.progressRequest; // the date you already commented/ posted
    const date2: any = this._processingDeliveryViewDto.deliveryDate; // today

    let r = {}; // object for clarity
    let message: string;

    const diffInSeconds = (date2 - date1) / 1000;
    const days = Math.floor(diffInSeconds / 60 / 60 / 24);
    if (days > 0) { //逾期
      this._processingDeliveryViewDto.overdueDay = days;
      this._processingDeliveryViewDto.isOverdue = true;
    } else {
      this._processingDeliveryViewDto.overdueDay = 0;
      this._processingDeliveryViewDto.isOverdue = false;
    }
  }

}
